package edu.calpoly.android.lab2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class SimpleJokeList extends Activity {

	/** Contains the list Jokes the Activity will present to the user **/
	protected ArrayList<Joke> m_arrJokeList;

	/**
	 * LinearLayout used for maintaining a list of Views that each display Jokes
	 **/
	protected LinearLayout m_vwJokeLayout;

	/**
	 * EditText used for entering text for a new Joke to be added to
	 * m_arrJokeList.
	 **/
	protected EditText m_vwJokeEditText;

	/**
	 * Button used for creating and adding a new Joke to m_arrJokeList using the
	 * text entered in m_vwJokeEditText.
	 **/
	protected Button m_vwJokeButton;
	
	/**
	 * Background Color values used for alternating between light and dark rows
	 * of Jokes.  
	 */
	protected int m_nDarkColor;
	protected int m_nLightColor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		this.initLayout();
	}
	
	/**
	 * Method used to encapsulate the code that initializes and sets the Layout
	 * for this Activity. 
	 */
	protected void initLayout() {
		this.m_nDarkColor = R.color.dark;
		this.m_nLightColor= R.color.light;
		
		this.m_vwJokeLayout = new LinearLayout(this);
		this.m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL);
		this.m_arrJokeList = new ArrayList<Joke>();
		
		this.m_vwJokeEditText = new EditText(this);
		
		this.m_vwJokeButton = new Button(this);
		
		Resources res = this.getResources();
		
		
		/**
		 * @var main layout
		 */
		LinearLayout root = new LinearLayout(this);
		root.setOrientation(LinearLayout.VERTICAL);
		/**
		 * @var layout with controls
		 */
		LinearLayout controls = new LinearLayout(this);
		controls.setOrientation(LinearLayout.HORIZONTAL);
		/**
		 * scrollable view with jokes
		 */
		ScrollView scrollable = new ScrollView(this);
		
		this.m_vwJokeButton.setText("evilevil");
		
		this.initAddJokeListeners();
		controls.addView(m_vwJokeButton);
		controls.addView(m_vwJokeEditText);
		
		
		root.addView(controls);
		root.addView(scrollable);
		
		scrollable.addView(m_vwJokeLayout);
		setContentView(root);
		
		for(String jk:res.getStringArray(R.array.jokeList))
			this.addJoke(jk);
		
		// TODO
	}
	
	/**
	 * Method used to encapsulate the code that initializes and sets the Event
	 * Listeners which will respond to requests to "Add" a new Joke to the 
	 * list. 
	 */
	protected void initAddJokeListeners() {
		this.m_vwJokeButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				addJokeFromInterface();
			}
		});
		
		this.m_vwJokeEditText.setOnKeyListener(
				new OnKeyListener() {
					
					@Override
					public boolean onKey(View v, int keyCode, KeyEvent event) {
						if(KeyEvent.KEYCODE_ENTER!=keyCode)
							return false;
						if(event.getAction()!=KeyEvent.ACTION_DOWN)
							return false;
						addJokeFromInterface();
						return false;
					}
				
				
		});
	}

	
	protected void addJokeFromInterface()
	{
		addJoke(m_vwJokeEditText.getText().toString());
		InputMethodManager imm = (InputMethodManager)
				  getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
				m_vwJokeEditText.setText("");
	}
	/**
	 * Method used for encapsulating the logic necessary to properly initialize
	 * a new joke, add it to m_arrJokeList, and display it on screen.
	 * 
	 * @param strJoke
	 *            A string containing the text of the Joke to add.
	 */
	protected void addJoke(String strJoke) {
		Joke tmp = new Joke(strJoke);
		this.m_arrJokeList.add(tmp);
		TextView tmp_view = new TextView(this);
		tmp_view.setText(tmp.toString());
		tmp_view.setBackgroundColor(this.m_nDarkColor);
		this.m_vwJokeLayout.addView(tmp_view);
		tmp_view.setTextSize(16);
	}
}